package com.th.apicenter.toutiao.service;

import com.google.gson.Gson;
import com.th.apicenter.kuaishou.service.KuaiShouService;
import com.th.apicenter.tools.OkhttpClient;
import com.th.apicenter.toutiao.dto.ToutiaoConversionDTO;
import com.th.apicenter.toutiao.entity.ThTtData;
import com.th.apicenter.toutiao.mapper.ThTtDataMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;

/**
 * @author kay
 * @date 2025/6/16
 * @TODO
 */
@Service
@Slf4j
public class ToutiaoService {

    @Resource
    private ThTtDataMapper thTtDataMapper;

    @Autowired
    private KuaiShouService kuaiShouService;

    public void monitor(ThTtData thTtData) {
        thTtDataMapper.insert(thTtData);
        log.info("头条检测接收到的数据：{}", thTtData);
        kuaiShouService.clickToutiao(thTtData, thTtData.getAdid().toString());
    }

    public void callback(String actionType, String TTId) throws IOException {
        String cvType = "";
        switch (actionType) {
            case "1":
                cvType = "active";
                break;
            case "2":
                cvType = "next_day_open";
                break;
            default:
                break;
        }
        if (!"".equals(cvType)) {
            ToutiaoConversionDTO toutiaoConversionDTO = new ToutiaoConversionDTO();
            ThTtData thTtData = thTtDataMapper.selectById(TTId);
            if (null == thTtData.getKsActionType()) {
                thTtData.setKsActionType(actionType);
            } else if (null != thTtData.getKsActionType() && null == thTtData.getKsActionTypeO()) {
                thTtData.setKsActionTypeO(actionType);
            }
            toutiaoConversionDTO.setEvent_type(cvType);
            ToutiaoConversionDTO.Ad ad = new ToutiaoConversionDTO.Ad();
            ad.setCallback(thTtData.getCallbackParam());
            ToutiaoConversionDTO.Context context = new ToutiaoConversionDTO.Context();
            context.setAd(ad);
            ToutiaoConversionDTO.Device device = new ToutiaoConversionDTO.Device();
            if (thTtData.getOs().equals("0")) {
                device.setPlatform("android");
                device.setOaid(thTtData.getOaid());
            } else {
                device.setPlatform("ios");
                device.setIdfa(thTtData.getIdfa());
            }
            context.setDevice(device);
            toutiaoConversionDTO.setContext(context);
            toutiaoConversionDTO.setTimestamp(System.currentTimeMillis());
            String toutiaoGson = new Gson().toJson(toutiaoConversionDTO);
            String toutiaoResult = OkhttpClient.sendPostRequest("https://analytics.oceanengine.com/api/v2/conversion", toutiaoGson);
            System.out.println(toutiaoResult);
            thTtData.setResultData(toutiaoResult);
            thTtData.setUpdateDate(new Date());
            thTtDataMapper.updateById(thTtData);
        }
    }
}
